﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace Librarian.Shared
{
    public static class StringHelpers
    {
#if PocketPC
        public static string RemoveDiacritics(this string stIn)
        {
            byte[] tabString = System.Text.Encoding.GetEncoding(1251).GetBytes(stIn);
            return System.Text.Encoding.ASCII.GetString(tabString,0,tabString.Length);
        }
#else
        public static string RemoveDiacritics(this string stIn)
        {
            string stFormD = stIn.Normalize(NormalizationForm.FormD);
            StringBuilder sb = new StringBuilder();

            for (int ich = 0; ich < stFormD.Length; ich++)
            {
                UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(stFormD[ich]);
                if (uc != UnicodeCategory.NonSpacingMark)
                {
                    sb.Append(stFormD[ich]);
                }

            }

            return sb.ToString().Normalize(NormalizationForm.FormC);
        }
#endif

        public static string RemoveAccents(this string s)
        {
            string vowels="aeiouAEIOU";
            string newValue = RemoveDiacritics(s);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < s.Length; i++)
            {
                if(vowels.Contains(newValue[i]))
                    sb.Append(newValue[i]);
                else
                    sb.Append(s[i]);
            }
            return sb.ToString();
        }
    }
}
